package ie.lawlor.amvc.example.explorer.productlist;

import ie.lawlor.amvc.event.Event;
import ie.lawlor.amvc.patterns.table.TableModel;
import tmp.AmazonOrder;
import tmp.AmazonProduct;
import tmp.AmazonVariant;


/**
 * @author blawlor
 *
 */
public class ProductListModel extends TableModel {

	public ProductListModel(String name){
		super(name);
		list.add(createProduct1());
		AmazonProduct p2 =createProduct2(); 
		list.add(p2);
		p2.addOrder(createOrder1());
		initFields();
	}

	private AmazonOrder createOrder1() {
		AmazonOrder o;
		o = new AmazonOrder();
		o.setOrderId("9876");
		o.setOrderTotal("13.00");
		return o;
	}

	private AmazonProduct createProduct2() {
		AmazonProduct p = new AmazonProduct();
		AmazonOrder o = new AmazonOrder();
		o.setOrderId("1234");
		o.setOrderTotal("25.00");
		p.addOrder(o);
		o = new AmazonOrder();
		o.setOrderId("5678");
		o.setOrderTotal("135.00");
		p.addOrder(o);
		p = new AmazonProduct();
		p.setSKU("123-zxc");
		p.setActive(true);
		p.setTitle("Something else");
		p.setVariationTheme("Color");
		AmazonVariant v1;
		AmazonVariant v2;
		v1 = new AmazonVariant();
		v1.setSKU("123-asd");
		v1.setTitle("Something else blue");
		v1.setActive(true);
		v1.setVariation("blue");
		v2 = new AmazonVariant();
		v2.setSKU("123-qwe");
		v2.setTitle("Something else green");
		v2.setActive(false);
		v2.setVariation("green");
		p.addVariant(v1);
		p.addVariant(v2);
		return p;
	}

	private AmazonProduct createProduct1() {
		AmazonProduct p = new AmazonProduct();
		p.setSKU("123-456");
		p.setActive(true);
		p.setTitle("Stoprite brake fluid");
		p.setVariationTheme("Size");
		AmazonVariant v1 = new AmazonVariant();
		v1.setSKU("123-001");
		v1.setTitle("Stoprite brake fluid 100ml");
		v1.setActive(true);
		v1.setVariation("100ml");
		AmazonVariant v2 = new AmazonVariant();
		v2.setSKU("123-002");
		v2.setTitle("Stoprite brake fluid 200ml");
		v2.setActive(false);
		v2.setVariation("200ml");
		p.addVariant(v1);
		p.addVariant(v2);
		return p;
	}

	private void initFields(){
		Field field = new Field(1, true);
		field.setTitle("SKU");
		fields.add(field);
		visibleFields.add(field);
		field = new Field(2, true);
		field.setTitle("Title");
		fields.add(field);
		visibleFields.add(field);
		field = new Field(3, true);
		field.setTitle("Active");
		fields.add(field);
		visibleFields.add(field);
		field = new Field(4, true);
		field.setTitle("Variation Theme");
		fields.add(field);
		visibleFields.add(field);
	}

	public void doList(Event e){
		fireUpdateView();
	}

	public int getMaxWidth(int columnIndex) {
		switch (columnIndex){
		case 0: return 12; 
		case 1: return 100; 
		case 2: return 5;
		case 3: return 15;
		default: return 10;
		}
	}

	public int getNumberOfFields() {
		return 4;
	}

	public int getPreferredWidth(int columnIndex) {
		switch (columnIndex){
		case 0: return 12; 
		case 1: return 25; 
		case 2: return 5;
		case 3: return 15;
		default: return 10;
		}
	}

	public Object getValueAt(int x, int y) {
		AmazonProduct p = (AmazonProduct)list.get(x);
		switch (y){
		case 0: return p.getSKU(); 
		case 1: return p.getTitle(); 
		case 2: return p.isActive()?"Y":"N";
		case 3: return p.getVariationTheme();
		default: return null;
		}
	}
}
